package com.pj.point.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.pj.point.R;
import com.pj.point.litepal.CreateData;
import com.pj.point.litepal.DiaryJavaBean;

import java.util.List;

public class HomeListAdapter extends MyAdapter {

    List<DiaryJavaBean> data;
    private TextView homeItemTitle;
    private TextView homeItemCreateTime;
    private ImageView homeItemImg;

    public HomeListAdapter(List<DiaryJavaBean> data) {
        super(data);
        this.data = data;
    }

    @Override

    public View getView(int i, View view, ViewGroup viewGroup) {
        @SuppressLint("ViewHolder") View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.home_item, null);
        initView(v);
        DiaryJavaBean bean = data.get(i);
        homeItemTitle.setText(bean.getTitle().equals("") ? "未命名" : bean.getTitle());
        homeItemCreateTime.setText(bean.getCreateTime());
        if (bean.getContent() != null)
            for (CreateData createData : bean.getContent()) {
                if (createData.getKey().equals("image")) {
                    Glide.with(viewGroup.getContext()).load(createData.getValue()).into(homeItemImg);
                    break;
                }
            }
        return v;
    }

    private void initView(View v) {
        homeItemTitle = (TextView) v.findViewById(R.id.home_item_title);
        homeItemCreateTime = (TextView) v.findViewById(R.id.home_item_create_time);
        homeItemImg = (ImageView) v.findViewById(R.id.home_item_img);
    }
}
